<template>
  <a-row id="globalHeader" :wrap="false" align="center">
    <!-- 菜单区域 -->
    <a-col flex="auto">
      <a-menu
        :selected-keys="selectedKeys"
        mode="horizontal"
        @menu-item-click="doMenuClick"
      >
        <!-- Logo 和标题 -->
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="titleBar">
            <img class="logo" src="../assets/logo.png" />
            <div class="title">来福滴答</div>
          </div>
        </a-menu-item>
        <!-- 动态加载菜单 -->
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>

    <!-- 用户信息区域 -->
    <a-col flex="300px">
      <a-space align="center">
        <!-- 用户登录时 -->
        <template v-if="loginUserStore.loginUser.id">
          <a-space align="center">
            <Avatar size="40">
              <img
                :src="
                  loginUserStore.loginUser.userAvatar ||
                  'https://bu.dusays.com/2021/03/03/365c96d0cdf9a.png'
                "
                alt="avatar"
              />
            </Avatar>

            <span class="user-name">{{
              loginUserStore.loginUser.userName ?? "匿名用户"
            }}</span>
          </a-space>
          <a-popconfirm
            content="是否登出"
            position="bottom"
            type="warning"
            @ok="logout"
          >
            <a-button size="small" type="outline" class="logout-button"
              >登出
            </a-button>
          </a-popconfirm>
        </template>

        <!-- 用户未登录时 -->
        <template v-else>
          <a-button href="/user/login" type="primary">登录</a-button>
        </template>
      </a-space>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useLoginUserStore } from "@/store/userStore";
import checkAccess from "@/access/checkAccess";
import { Avatar } from "@arco-design/web-vue";
import message from "@arco-design/web-vue/es/message";
import { userLogoutUsingPost } from "@/api/userController";

const loginUserStore = useLoginUserStore();

const router = useRouter();
// 当前选中的菜单项
const selectedKeys = ref(["/"]);
// 路由跳转时，自动更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

// 展示在菜单栏的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (!checkAccess(loginUserStore.loginUser, item.meta?.access as string)) {
      return false;
    }
    return true;
  });
});

// 点击菜单跳转到对应页面
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

const logout = async () => {
  const res = await userLogoutUsingPost();
  if (res.data.code === 0) {
    message.success("退出成功");
  } else {
    message.error("退出失败，" + res.data.message);
  }
  //刷新页面的函数
  window.location.reload();
};
</script>

<style scoped>
#globalHeader {
  padding: 0 16px; /* 左右内边距 */
  background-color: #ffffff; /* 背景白色 */
  border-bottom: 1px solid #f0f0f0; /* 底部细线 */
}

.titleBar {
  display: flex;
  align-items: center;
}

.title {
  margin-left: 16px;
  color: #333;
  font-weight: bold;
  font-size: 18px;
}

.logo {
  height: 48px;
}

.user-name {
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.logout-button {
  padding: 4px 12px;
  font-size: 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.logout-button:hover {
  color: #fff;
  background-color: #f53f3f; /* Arco Design 的红色 */
  border-color: #f53f3f;
}
</style>
